package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.entity.Employee;
import com.example.reggie.service.EmployeeService;
import com.example.reggie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        String username = employee.getUsername();

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Employee> eq = lqw.eq(Employee::getUsername, username);
        Employee emp = employeeService.getOne(eq);
        if (emp==null){
            return R.error("登录失败，用户名不存在");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("登录失败，密码错误");
        }
        if (emp.getStatus()==0){
            return R.error("登录失败，该用户已被锁定");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }


    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R add(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工信息:"+employee.toString());
        //设置初始化默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));


        employeeService.save(employee);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(name),Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);
        IPage page1 = new Page(page,pageSize);
        employeeService.page(page1, lqw);
        return R.success(page1);
    }

    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){

//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable long id){
        Employee emp = employeeService.getById(id);
        return R.success(emp);

    }
}
